package com.elite.groovy.callback

import cn.com.tpUtil.GetXMLValue
import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class autoDataComplete implements ServiceBackInvoke {
	private static final Log log=LogFactory.getLog(autoDataComplete.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

		//调用蚂蚁信息补全接口地址
		def seleteSQL="select webserviceurl from sysc_interface   where webtype='SJBQ'";
			def result=eliteBean.chaxun_sql(seleteSQL, "dataSource");
			def queryMap=result[0];
			def httpURL = queryMap.get("webserviceurl");
		log.debug("接口地址:"+httpURL);
		//def httpURL="http://10.0.98.211:7004/EliteGeneralHttpTask/api/mayiAPITest";
		//def httpURL="http://10.0.100.76:7001/react/dx";
		//生产环境地址
		//def httpURL="http://10.94.28.51:8001/react/dx"; 

		//接口调用配置信息
		def charset="UTF-8";
		def timeout=30000;
		def enableSSLflag=0;

		//获取本次补全参数
		def task_guid = map.get("task_guid");
		def quote_no = map.get("quote_no");

		//打印发送参数信息
		log.debug("task_guid:"+task_guid);
		log.debug("quote_no:"+quote_no);

		//拼接发送内容
		def sendBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><PACKET TYPE=\"REQUEST\" VERSION=\"1.0\"><HEAD><REQUEST_TYPE>SJBQ</REQUEST_TYPE></HEAD><BODY><QUOTE_NO>" + quote_no + "</QUOTE_NO></BODY></PACKET>";
		log.debug("系统拼接后的发送报文Body:"+sendBody);

		def REQUEST_TYPE;
		def RESPONSE_CODE;
		def ERROR_MESSAGE;

		def insertSQL;
		def updateSQL;
		def deal_flag;



		try {
			def httpResult = Util.doHttpPost(httpURL, sendBody, charset, timeout, enableSSLflag);
			log.debug("调用补全接口返回信息:"+httpResult);

			//解析返回结果
			REQUEST_TYPE = GetXMLValue.readStringXml(httpResult, "REQUEST_TYPE");
			RESPONSE_CODE = GetXMLValue.readStringXml(httpResult, "RESPONSE_CODE");
			ERROR_MESSAGE = GetXMLValue.readStringXml(httpResult, "ERROR_MESSAGE");

			log.debug("解析詳情返回參數REQUEST_TYPE:"+REQUEST_TYPE);
			log.debug("解析詳情返回參數RESPONSE_CODE:"+RESPONSE_CODE);
			log.debug("解析詳情返回參數ERROR_MESSAGE:"+ERROR_MESSAGE);

			if(REQUEST_TYPE.equals("SJBQ")&&RESPONSE_CODE.equals("1"))
			{
                def i_checkintime    = GetXMLValue.readStringXml(httpResult, "checkintime");
				log.debug("调用补全接口处理checkintime2:"+i_checkintime);
				if(i_checkintime==null){i_checkintime='';}
				log.debug("调用补全接口处理checkintime3:"+i_checkintime);
			    insertSQL="update WZD_CUSTOMER_DSF_EXTEND"+
							"   set offer_no       = '" + GetXMLValue.readStringXml(httpResult, "QUOTE_NO") + "',"+
							"       quote_no       = '" + GetXMLValue.readStringXml(httpResult, "QUOTE_NO") + "',"+
							"       proposal_no    = '" + GetXMLValue.readStringXml(httpResult, "QUOTE_NO") + "',"+
							"       customername   = '" + GetXMLValue.readStringXml(httpResult, "CUSTOMERNAME") + "',"+
							"       idtype         = '" + GetXMLValue.readStringXml(httpResult, "IDTYPE") + "',"+
							"       customerid     = '" + GetXMLValue.readStringXml(httpResult, "CUSTOMERID") + "',"+
							"       frame_code     = '" + GetXMLValue.readStringXml(httpResult, "FRAME_CODE") + "',"+
							"       modelcode      = '" + GetXMLValue.readStringXml(httpResult, "MODELCODE") + "',"+
							"       in_startdate   = TO_DATE('" + GetXMLValue.readStringXml(httpResult, "IN_STARTDATE") + "','yyyy-mm-dd hh24:mi:ss'),"+
							"       engine_code    = '" + GetXMLValue.readStringXml(httpResult, "ENGINE_CODE") + "',"+
							"       brand_name     = '" + GetXMLValue.readStringXml(httpResult, "BRAND_NAME") + "',"+
							"       applicant_name = '" + GetXMLValue.readStringXml(httpResult, "APPLICANT_NAME") + "',"+
							"       insured_name   = '" + GetXMLValue.readStringXml(httpResult, "INSURED_NAME") + "',"+
							"       sales_person_orgid   = '" + GetXMLValue.readStringXml(httpResult, "SALES_PERSON_ORGID") + "',"+
							"       sales_person_orgname = '" + GetXMLValue.readStringXml(httpResult, "SALES_PERSON_ORGNAME") + "',"+
							"       riskcode       = '" + GetXMLValue.readStringXml(httpResult, "RISKCODE") + "',"+
							"       startdate      = TO_DATE('" + GetXMLValue.readStringXml(httpResult, "STARTDATE") + "','yyyy-mm-dd hh24:mi:ss'),"+
							"       enddate        = TO_DATE('" + GetXMLValue.readStringXml(httpResult, "ENDDATE") + "','yyyy-mm-dd hh24:mi:ss'),"+
							"       checkintime    = TO_DATE('" + i_checkintime + "','yyyy-mm-dd hh24:mi:ss'),"+
							"       modified_date  = sysdate"+
							" where task_guid = '" + task_guid + "'" ;
				log.debug("调用补全接口处理成功SQL:"+insertSQL);
				eliteBean.update(insertSQL,'dataSource');

				deal_flag = 1;


			}else {
				deal_flag = 0;
			}

			updateSQL=" update WZD_CUSTOMER_DSF_EXTEND w set w.customer_flag = '" + deal_flag + "' where w.task_guid = '" + task_guid + "'";
			log.debug("将本次补全结果及次数加1SQL:"+updateSQL);
			eliteBean.update(updateSQL,'dataSource');


		}
		catch (Exception e)
		{
			log.error("autoDataComplete Exception ErrorMessage",e);
			code = "-1";
			message= "调用异常请检查或联系管理员！";
		}
	}
}